import pytest     #导入pytest模块
import requests   #导入库
import logging    #导入日志
import json,yaml  #数据驱动
#定义json数据驱动
# def data_json():
#     with open('data.json','r',encoding='utf8')as f:
#         return json.load(f)
# #定义yaml数据驱动
# def data_yaml():
#     with open('data.yaml','r',encoding='utf8')as f:
#         return yaml.safe_load(f)
#定义一个类
class TestCase():
    #提取变量token
    token = None
    #定义方法
    def setup_method(self): #测试前执行
        print("测试前执行方法")
    def teardown_method(self): #结束后
        print("结束后执行")


    #登录接口
    def test_login(self):
        try:
            url ="http://10.59.9.21/api/user/login"
            head ={"content-type":"application/json"}
            data ={"username":"admin","password":"8i4De25KlVQVwqTDgTNGqw=="}
            res = requests.post(url=url,headers=head,json=data)
            print(res.json())
            logging.info(res.json())
            #提取token变量让新增代理商使用
            TestCase.token = res.json()['token']
        except Exception as e:
            logging.info(e)
    #
    #     #断言
    #     assert "登录成功" in res.text

    # @pytest.mark.parametrize('d',data_yaml())
    #1、新增代理商接口所有参数信息正确且新增成功
    def test_case001(self):
        try:
            url ="http://10.59.9.21/api/user/register"
            head ={"Content-type":"application/json","uid": "1","token":TestCase.token}
            data ={"username":"opop3","password":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","checkPwd":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","a_id":"南口镇7","dtype":2,"phone":"13811033967","deadline":"2025-12-30"}
            res = requests.post(url=url,headers=head,json=data)
            print(res.json())
            logging.info(res.json())
        except Exception as e:
            logging.info(e)
        #断言
        # assert res.json()['msg'] == '登录成功'

    #2、新增代理商接口，未登录用户进行操作，新增失败
    def test_case002(self):
        try:
            url ="http://10.59.9.21/api/user/register"
            head ={"content-type":"application/json","uid": "1","token":TestCase.token}
            data ={"checkPwd":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","a_id":"南口镇1","dtype":2,"phone":"13811033969","deadline":"2025-12-30"}
            res = requests.post(url=url,json=data)   #没有请求头
            print(res.json())
            logging.info(res.json())
        except Exception as e:
            logging.info(e)
        #断言
    #     assert res.status_code == 200
    #
    #3、新增代理商接口，手机号参数为非1开头，新增失败
    def test_case003(self):
        try:
            url ="http://10.59.9.21/api/user/register"
            head ={"content-type":"application/json","uid": "1","token":TestCase.token}
            data ={"username":"pppp","password":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","checkPwd":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","a_id":"南口镇1","dtype":2,
                   "phone":"63811033969","deadline":"2025-12-30"}
            res = requests.post(url=url,headers=head,json=data)
            print(res.json())
            logging.info(res.json())
        except Exception as e:
            logging.info(e)
    #
    #     # 断言
    #     assert res.json()['code'] == 1062
    #
    #4、新增代理商接口，地址参数包含中文、英文且新增成功
    def test_case004(self):
        try:
            url ="http://10.59.9.21/api/user/register"
            head ={"content-type":"application/json","uid": "1","token":TestCase.token}
            data ={"username":"oooo","password":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","checkPwd":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","a_id":"南口镇a","dtype":2,"phone":"63811033969","deadline":"2025-12-30"}
            res = requests.post(url=url,headers=head,json=data)
            print(res.json())
            logging.info(res.json())
        except Exception as e:
            logging.info(e)
    #
    #     #断言
    #     assert res.json()['msg'] == '用户名已存在'
    #5、新增代理商接口，密码参数为空，新增失败
    def test_case005(self):
        try:
            url ="http://10.59.9.21/api/user/register"
            head ={"content-type":"application/json","uid": "1","token":TestCase.token}
            data ={"username":"opop1","password":"","checkPwd":"5aPdAgr8B2cPWFjFZTY4Kx7SDTGkq9qA+f944it96DY=","a_id":"南口镇a","dtype":2,"phone":"63811033969","deadline":"2025-12-30"}
            res = requests.post(url=url,headers=head,json=data)
            print(res.json())
            logging.info(res.json())
        except Exception as e:
            logging.info(e)
    #
    #     assert "密码不能为空" in res.text